<script setup>
import {computed, inject, provide, ref, watch,h} from "vue";
import delivery from '@/api/warehouse/delivery'
import BaseInfo from "./components/baseInfo.vue";
import WbParcels from "./components/WbParcels.vue";
import ExpenseInfo from "@/views/components/expenseInfo.vue";
import RouteInfo from "@/views/components/routeInfo.vue";
import { auth, role } from '@/utils/common'
import {request} from "@/utils/request.js";
import tool from '@/utils/tool'
import {Message} from "@arco-design/web-vue";
const routeInfo = ref({type:"delivery",about_type:'App\\Warehouse\\Model\\Delivery'})
const routeSpan = ref({lg: 6})
const record = ref({})
const visible = ref(false)
const crudRef = inject('crudRef')
const span = ref(24)
const invoicesType = ref('delivery')
const detailLoading = ref(true)
const open = async (data) => {
  record.value.id = data.id
  visible.value = true
  detailLoading.value = true
  await refreshRecode();
  detailLoading.value = false
}

const refreshRecode = async (type) => {
  if(record.value.id == undefined){
    return;
  }
  console.log('刷新详情')
  let request = await delivery.read(record.value.id)
  record.value = request.data
  record.value.turn =false
  return record
  // if(type == true){
  //   console.log('刷新列表')
  //   crudRef.value.refresh()
  // }
}
const shipmentExecl = () =>{
  Message.info('请求服务器下载文件中...')
  let data = {ids:[record.value.id]}
  const download = (url) => request({url,data , method: 'post', timeout: 60 * 1000, responseType: 'blob'})
  
  download('warehouse/delivery/shipmentExecl').then(res => {
    tool.download(res)
    Message.success('请求成功，文件开始下载')
  })
}
const close = () => {
  visible.value = false
}
const colGap = ref(6)
const rowGap = ref(6)
provide('colGap', colGap)
provide('rowGap', rowGap)
provide('record', record)
provide('refreshRecode', refreshRecode)
provide('invoicesType', invoicesType)
provide('span', span)
provide('routeInfo',routeInfo)
provide('routeSpan',routeSpan)
defineExpose({ open,refreshRecode })
</script>
<template>
  <a-drawer v-model:visible="visible" width="80%" v-if="visible" @cancel="close"
  :footer="false" unmount-on-close>
    <template #title v-if="!detailLoading">
      <div class="grid grid-cols-[minmax(0,1fr),auto] items-center">
        <div>转运单#{{ record.id }}</div>
        <div>
          <a-button v-auth="['warehouse:delivery:shipmentExecl']" @click="shipmentExecl()"><icon-download/>装柜单导出</a-button>
        </div>
      </div>
    </template>
      <a-spin v-if="detailLoading" :loading="detailLoading" tip="数据加载中..." style="text-align: center;display: block;margin-top: 100px;">
  </a-spin>
      <a-space direction="vertical" class="w-full"  v-if="!detailLoading">
        <a-grid :cols="{ xs: 1, sm: 2, md: 3, lg: 4, xxl: 6 }" :colGap="colGap" :rowGap="rowGap">
            <base-info />
            <wbParcels />
            <expenseInfo v-if="auth('finance:supplierFee:index')" />
            <RouteInfo />
        </a-grid>
      </a-space>
  </a-drawer>
</template>